﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CourierManager;
using System.IO;
using TeamWPF.Simulator;
using TeamWPF.Extensions;


namespace TeamWPF.Reports
{
    /// <summary>
    /// Interaction logic for Reports.xaml
    /// </summary>
    public partial class ReportsWindow : Window
    {

        List<string> allRecords = new List<string>();
        ObservableCollection<Freight> freightGridRows = new ObservableCollection<Freight>();
        ObservableCollection<Driver> driversGridRows = new ObservableCollection<Driver>();
        List<string> allDriversAsString = new List<string>();
        List<string> allFreightsAsString = new List<string>();
        List<string> allVehiclesAsString = new List<string>();
        ObservableCollection<TransportUnit> allVehicles = new ObservableCollection<TransportUnit>();
        public ReportsWindow()
        {
            InitializeComponent();
            //read all freights
            string path = "../../../CourierManager/ListOfBillOfLadings.txt";
            Freight freight = new Freight(path);
            allFreightsAsString = freight.AllBills;
            foreach (string el in allFreightsAsString)
            {   
                freightGridRows.Add(Simulator.Simulator.ReturnFreightFromString(el));
            }

            //Tracks and vans
            TransportUnit currVehicle = new TransportUnit();
            allVehiclesAsString = currVehicle.GetAllTransportUnits();
            foreach (string vehicle in allVehiclesAsString)
            {
                allVehicles.Add(Simulator.Simulator.ReturnVehicleFromString(vehicle));
            }
            //all drivers
            Driver currDriver = new Driver();
            allDriversAsString = currDriver.ReadEmployeeData();
            foreach (string driverasstring in allDriversAsString)
            {
                if (driverasstring.Split('*')[0] == currDriver.GetType().Name)
                {
                 driversGridRows.Add(Simulator.Simulator.ReturnDriverFromString(driverasstring.Split('*')[4]));                  
                }
            }
        }

        private void dgFreights_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

        private void cmbreporttype_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selecteditemvalue = this.cmbreporttype.SelectedValue.ToString();

            if (selecteditemvalue != null)
            {
                if (selecteditemvalue.ToLower() == "allvehicles")
                {
                    this.vehicles.DataContext = allVehicles;
                    this.vehicles.Visibility = Visibility.Visible;
                    this.dgFreights.Visibility = Visibility.Hidden;
                    this.drivers.Visibility = Visibility.Hidden;
                }
                else if (selecteditemvalue.ToLower() == "allfreights")
                {
                    this.dgFreights.DataContext = freightGridRows;
                    this.dgFreights.Visibility = Visibility.Visible;
                    this.vehicles.Visibility = Visibility.Hidden;
                    this.drivers.Visibility = Visibility.Hidden;
                }
                else if (selecteditemvalue.ToLower() == "alldrivers")
                {
                    this.drivers.DataContext = driversGridRows;
                    this.drivers.Visibility = Visibility.Visible;
                    this.dgFreights.Visibility = Visibility.Hidden;
                    this.vehicles.Visibility = Visibility.Hidden;
                }
            }

        }

        private void vehicles_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

        private void drivers_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            e.Row.Header = (e.Row.GetIndex() + 1).ToString();
        }

    }
}
